import os
import shutil
from PIL import Image


# 通过扩展名判断文件是不是图片
def ispic(fpath):
    f_full_path = os.path.join(dir_path, fpath)
    ispicflag = False
    if not os.path.isdir(f_full_path):
        pic_ext = [".jpg", ".png", ".gif", ".bmp", ".tiff", ".webp"]
        for p in pic_ext:
            if fpath.endswith(p):
                ispicflag = True
    return ispicflag


# 获取长边长度
def getlongside(width, height):
    if width >= height:
        return width
    else:
        return height


# 移动小图到文件夹
def takeinsmall(fname):
    smalldir = os.path.join(dir_path, "small")
    if not os.path.exists(smalldir):
        os.makedirs(smalldir)
    shutil.move(dir_path + "\\" + fname, smalldir + "\\" + fname)
    print("过滤小图", fname)


dir_path = input("输入要过滤的文件夹路径 ")
# dir_path = "D:\\User\\Pictures"
file_list = os.listdir(dir_path)
small_file_num = 0
for f in file_list:
    if ispic(f):
        full_path = os.path.join(dir_path, f)
        img = Image.open(full_path)
        w, h = img.size
        img.close()
        # print(str(w) + "x" + str(h), f)
        longside = getlongside(w, h)
        if longside < 600:
            takeinsmall(f)
            small_file_num += 1
print("收起小图", small_file_num)
# img=Image.open(pic_path,"r")
# w,h=img.size
# print(w,h)
# img1=img.resize((w//2,h//2))
# img1.save("D:\\User\\Pictures\\xx1-2.jpg")
